﻿#include "HttpClient.h"
#include "HttpRequestHandler.h"
#include "httpserver.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::serverStartSlot(QTcpSocket *socket)
{
    QByteArray requestData = socket->readAll();
    ui->textEdit->setText(QString(requestData));


    /*QByteArray request = socket->readAll();
    QString responseContent = "Hello, World!";
    QString httpResponse = "HTTP/1.1 200 OK\r\n"
                           "Content-Length: " + QString::number(responseContent.length()) + "\r\n"
                           "Content-Type: text/plain\r\n"
                           "\r\n"
                           + responseContent;
    socket->write(httpResponse.toUtf8());
    socket->flush();
    socket->waitForBytesWritten();
    socket->close();*/
}

void MainWindow::on_btn_serverStart_clicked()
{
    HttpServer httpServer;
    HttpRequestHandler httpRequestHandler;
    QObject::connect(&httpServer, &HttpServer::requestReceived, &httpRequestHandler, &HttpRequestHandler::processRequest);
    if(httpServer.start(8080))
    {
        ui->textEdit->setText(QString("服务器监听启动成功"));
    }
    else
    {
         ui->textEdit->setText(QString("服务器监听启动失败"));
    }

}

void MainWindow::on_btn_serverStop_clicked()
{


}
void MainWindow::on_btn_clientGet_clicked()
{


}
void MainWindow::on_btn_ClientPost_clicked()
{

    HttpClient client;
    QNetworkReply* reply = client.postRequst("localhost", "8080", "123");
    QByteArray responseData = reply->readAll();
     ui->textEdit->setText(QString(responseData));
}
